/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.dto.ProductDockedDto;
import com.yami.shop.bean.model.ProductDocked;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ProductDockedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 已对接商品控制器
 */
@RestController
@RequestMapping("/prod/docked")
@Api(tags = "代理模块-已对接商品接口")
public class ProductDockedController {

    @Autowired
    private ProductDockedService productDockedService;

    
    /**
     * 高级搜索已对接商品列表
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页已对接商品列表", notes = "根据条件搜索已对接商品列表")
    public ResponseEntity<IPage<ProductDockedDto>> searchProductDockedPage(
            PageParam<ProductDockedDto> page, 
            @RequestBody(required = false) ProductDockedDto productDockedDto) {
        // 设置当前店铺ID，防止查询到其他店铺的数据
        Long shopId = SecurityUtils.getShopUser().getShopId();
        if (productDockedDto == null) {
            productDockedDto = new ProductDockedDto();
        }
        productDockedDto.setShopId(shopId);
        
        IPage<ProductDockedDto> productDockedPage = productDockedService.searchProductDockedDtoPage(
                new Page<>(page.getCurrent(), page.getSize()), productDockedDto);
        return ResponseEntity.ok(productDockedPage);
    }

    /**
     * 根据分类ID获取已对接商品列表
     */
    @GetMapping("/page/category/{shopCategoryId}")
    @ApiOperation(value = "根据分类ID获取已对接商品列表", notes = "根据分类ID分页获取当前店铺的已对接商品列表")
    public ResponseEntity<IPage<ProductDockedDto>> getProductDockedPageByCategoryId(
            PageParam<ProductDockedDto> page,
            @ApiParam(value = "店铺分类ID") @PathVariable("shopCategoryId") Long shopCategoryId) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        IPage<ProductDockedDto> productDockedPage = productDockedService.getProductDockedDtoPageByCategoryId(
                new Page<>(page.getCurrent(), page.getSize()), shopCategoryId, shopId);
        return ResponseEntity.ok(productDockedPage);
    }

    /**
     * 根据上级店铺ID获取已对接商品列表
     */
    @GetMapping("/list/sup-shop/{supShopId}")
    @ApiOperation(value = "根据上级店铺ID获取已对接商品列表", notes = "根据上级店铺ID获取当前店铺的已对接商品列表")
    public ResponseEntity<List<ProductDocked>> getProductDockedListBySupShopId(
            @ApiParam(value = "上级店铺ID") @PathVariable("supShopId") Long supShopId) {
        // 验证当前店铺是否有权限查看
        Long shopId = SecurityUtils.getShopUser().getShopId();
        List<ProductDocked> productDockedList = productDockedService.getBySupShopId(supShopId);
        
        // 过滤只属于当前店铺的商品
        productDockedList.removeIf(item -> !item.getShopId().equals(shopId));
        
        return ResponseEntity.ok(productDockedList);
    }

    /**
     * 获取已对接商品详情
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取已对接商品详情", notes = "根据ID获取已对接商品详情")
    public ResponseEntity<ProductDockedDto> getProductDockedById(
            @ApiParam(value = "已对接商品ID") @PathVariable("id") Long id) {
        ProductDockedDto productDockedDto = productDockedService.getProductDockedDtoById(id);
        
        // 验证商品是否属于当前店铺
        Long shopId = SecurityUtils.getShopUser().getShopId();
        if (productDockedDto != null && !productDockedDto.getShopId().equals(shopId)) {
            throw new YamiShopBindException("无权查看该商品");
        }
        
        return ResponseEntity.ok(productDockedDto);
    }

    /**
     * 添加已对接商品
     */
    @PostMapping
    @ApiOperation(value = "添加已对接商品", notes = "添加已对接商品")
    public ResponseEntity<Void> addProductDocked(@RequestBody ProductDocked productDocked) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        productDocked.setShopId(shopId);
        
        // 验证必填字段
        if (productDocked.getProdId() == null) {
            throw new YamiShopBindException("商品ID不能为空");
        }
        if (productDocked.getShopCategoryId() == null) {
            throw new YamiShopBindException("店铺分类ID不能为空");
        }
        if (productDocked.getSupShopId() == null) {
            throw new YamiShopBindException("上级店铺ID不能为空");
        }
        
        boolean result = productDockedService.addProductDocked(productDocked);
        if (!result) {
            throw new YamiShopBindException("该商品已对接，请勿重复添加");
        }
        
        return ResponseEntity.ok().build();
    }

    /**
     * 更新已对接商品
     */
    @PutMapping
    @ApiOperation(value = "更新已对接商品", notes = "更新已对接商品信息")
    public ResponseEntity<Void> updateProductDocked(@RequestBody ProductDocked productDocked) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        
        // 验证商品是否属于当前店铺
        ProductDocked dbProductDocked = productDockedService.getById(productDocked.getId());
        if (dbProductDocked == null || !dbProductDocked.getShopId().equals(shopId)) {
            throw new YamiShopBindException("无权修改该商品");
        }
        
        // 设置店铺ID，防止篡改
        productDocked.setShopId(shopId);
        
        boolean result = productDockedService.updateProductDocked(productDocked);
        if (!result) {
            throw new YamiShopBindException("更新失败");
        }
        
        return ResponseEntity.ok().build();
    }

    /**
     * 更新商品状态
     */
    @PutMapping("/status/{id}")
    @ApiOperation(value = "更新商品状态", notes = "更新商品上下架状态")
    public ResponseEntity<Void> updateStatus(
            @ApiParam(value = "已对接商品ID") @PathVariable("id") Long id,
            @ApiParam(value = "状态，0为下架，1为上架") @RequestParam("status") Integer status) {
        
        Long shopId = SecurityUtils.getShopUser().getShopId();
        
        // 验证商品是否属于当前店铺
        ProductDocked dbProductDocked = productDockedService.getById(id);
        if (dbProductDocked == null || !dbProductDocked.getShopId().equals(shopId)) {
            throw new YamiShopBindException("无权修改该商品");
        }
        
        boolean result = productDockedService.updateStatus(id, status);
        if (!result) {
            throw new YamiShopBindException("更新状态失败");
        }
        
        return ResponseEntity.ok().build();
    }

    /**
     * 删除已对接商品
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除已对接商品", notes = "删除已对接商品")
    public ResponseEntity<Void> deleteProductDocked(
            @ApiParam(value = "已对接商品ID") @PathVariable("id") Long id) {
        
        Long shopId = SecurityUtils.getShopUser().getShopId();
        
        // 验证商品是否属于当前店铺
        ProductDocked dbProductDocked = productDockedService.getById(id);
        if (dbProductDocked == null || !dbProductDocked.getShopId().equals(shopId)) {
            throw new YamiShopBindException("无权删除该商品");
        }
        
        boolean result = productDockedService.deleteProductDocked(id);
        if (!result) {
            throw new YamiShopBindException("删除失败");
        }
        
        return ResponseEntity.ok().build();
    }
} 